package com.example.datn.customviews;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MusicBarButton extends LinearLayout{

	public MusicBarButton(Context context, int id, View view) {
		super(context);

		// if our context is not Activity we can't get View supplied by id
        if (!(context instanceof Activity))
            return;
        
        // find relative layout by id  
        View v = view.findViewById(id);
        
        //cast it to relative layout
        LinearLayout layout = (LinearLayout)v;
        
        // copy layout parameters 
        ViewGroup.LayoutParams params = layout.getLayoutParams();
        this.setLayoutParams(params);
        
        this.setBackgroundColor(Color.GRAY);
        
        while (layout.getChildCount() > 0)
        {
             View vchild = layout.getChildAt(0);
             layout.removeViewAt(0);
             this.addView(vchild);	             
             vchild.setClickable(true);
        }
        
     // remove all view from layout (maybe it's not necessary)
        layout.removeAllViews();
        // set that this button is clickable, focusable, ...
        this.setClickable(true);
        this.setFocusable(true);
        
        // replace relative layout in parent with this one modified to looks like button
        ViewGroup vp = (ViewGroup)layout.getParent();
        int index = vp.indexOfChild(layout);
        vp.removeView(layout);
        vp.addView(this,index);
         
        this.setId(id);
	}
	
	public void setText(int id, CharSequence text)
    {
        View v = findViewById(id);
        if (null != v && v instanceof TextView){
            ((TextView)v).setText(text);
        }
         
    }

}
